﻿#include "OperatorsOpen.h"
#include <QFile>
#include <QFileDialog>
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h" 
#include "FITK_Kernel/FITKAppFramework/FITKRunTimeSetting.h"
#include "FITK_Kernel/FITKCore/FITKThreadPool.h"
#include "FITK_Component/FITKAcousticsHDF5IO/FITKAcousticsHDF5IOInterface.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGroupData.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "GUIFrame/MainWindow.h"


#include "GUIFrame/MainWindow.h"
#include "GUIWidget/ControlPanel.h"
#include "GUIWidget/TreeWidgetGroup.h"
#include "GUIFrame/CentralWidget.h"
#include "GUIWidget/TreeWidgetLoad.h"
#include "GUIWidget/TreeItemTypes.h"
#include "GUIFrame/CentralWidget.h"

namespace ModelOper
{
    OperatorsOpen::OperatorsOpen()
    {

    }
    OperatorsOpen::~OperatorsOpen()
    {
    }
    bool OperatorsOpen::execGUI()
    {
        //主界面
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //获取工作目录
        QString wk;
        auto settings = FITKAPP->getAppSettings();
        if (settings)
            wk = settings->getWorkingDir();
        // QString fileName = QFileDialog::getOpenFileName(mw, QObject::tr("Open INP File"),"" , "INP(*.inp)");

        QFileDialog dlg(mw, QObject::tr("Open Project File"), wk, "HDF5(*.hdf5)");
        dlg.setAcceptMode(QFileDialog::AcceptOpen);  //打开模式
        dlg.show();
        //阻塞，否则键盘事件处理会出问题
        bool accept = false;
        QEventLoop loop;
        connect(&dlg, &QFileDialog::accepted, [&] {loop.quit(); accept = true;  });
        connect(&dlg, &QFileDialog::rejected, [&] {loop.quit(); accept = false;  });
        loop.exec();

        if (!accept) return false;
        QStringList files = dlg.selectedFiles(); //选择的文件名称
        if (files.isEmpty()) return false;
        QString fileName = files.at(0);

        if (fileName.isEmpty())
            return false;
        //存储信息
        if (_emitter == nullptr) return false;

        this->setArgs("FileName", fileName);
        return true;
    }
    bool OperatorsOpen::execProfession()
    {
        //文件存在
        QString fileName;
        bool ok = this->argValue<QString>("FileName", fileName);
        if (!ok) return false;
        QFileInfo finfo(fileName); 
        if (!finfo.exists()) return false;

        //输出信息
        AppFrame::FITKMessageNormal(QString(tr("Reading project file %1 ...")).arg(fileName));

        //读写组件获取
        IO::FITKAcousticsHDF5IOInterface* h5IO = FITKAPP->getComponents()->
            getComponentTByName<IO::FITKAcousticsHDF5IOInterface>("AcousticsHDF5IO");

        //等待线程结束
        Core::FITKThreadPool::getInstance()->wait();
        if (h5IO == nullptr) return false;

        GUI::MainWindow* mw = FITKAPP->getGlobalData()->getMainWindowT<GUI::MainWindow>();
        if (mw)
            mw->setProcessLabel(QString(tr("Reading %1")).arg(fileName));

        //获取要存储的数据位置
        Core::FITKAbstractDataObject* model =
            FITKAPP->getGlobalData()->getMeshData<Core::FITKAbstractDataObject>();
        Core::FITKAbstractDataObject* physics =
            FITKAPP->getGlobalData()->getPhysicsData<Core::FITKAbstractDataObject>();
        Core::FITKAbstractDataObject* geoData =
            FITKAPP->getGlobalData()->getGeometryData<Core::FITKAbstractDataObject>();
        Core::FITKAbstractDataObject* postData =
            FITKAPP->getGlobalData()->getPostData<Core::FITKAbstractDataObject>();
        Core::FITKAbstractDataObject* groupData =
            Acoustics::FITKAcousticsGroupManager::GetGroupManagerFromFrame();//声学分组数据管理器


        //传入参数
        h5IO->setDataObject("Model", model);
        h5IO->setDataObject("Physics", physics);
        h5IO->setDataObject("Geometry", geoData);
        h5IO->setDataObject("PostData", postData);
        h5IO->setDataObject("GroupData", groupData);
        h5IO->setFileName(fileName);
        h5IO->runInThread();
        //关联结束读取槽函数
        connect(h5IO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()), Qt::UniqueConnection);
        //执行读取
        h5IO->exec(1);

        //保存执行的脚本
        QString scr = QString("Global.Files.ImportHDF5File('%1')").arg(fileName);
        this->saveScript(scr);
        return false;
    }

    void OperatorsOpen::upGroupTreeAndTreeWidgetLoad()
    {
        //刷新组界面树
        GUI::MainWindow* mw = GUI::MainWindow::GetMainWindowFromFramework();
        if (mw == nullptr) return;
        GUI::CentralWidget* cw = mw->getCentralWidget();
        if (cw == nullptr) return;
        GUI::ControlPanel*  cp = cw->getControlPanel();
        if (cp == nullptr) return;

        //刷新分组树
        GUI::TreeWidgetGroup* GroupmodelTree = dynamic_cast<GUI::TreeWidgetGroup*>(cp->getTreeWidget(GUI::TreeWidgetType::PageGroup));
        if (GroupmodelTree == nullptr) return;
        GroupmodelTree->updateGroupTree();

        //刷新loadCase树
        GUI::TreeWidgetLoad* LoadTree = dynamic_cast<GUI::TreeWidgetLoad*>(cp->getTreeWidget(GUI::TreeWidgetType::PageLoad));
        if (LoadTree == nullptr) return;
        LoadTree->updateLoadCaseTree();
    }

    void OperatorsOpen::ioThreadFinishedSlot()
    {
        AppFrame::FITKRunTimeSetting* rts = FITKAPP->getGlobalData()->getRunTimeSetting();
        if (rts == nullptr) return;
        QString fileName;
        bool ok = this->argValue<QString>("FileName", fileName);
        if (!ok) return;
        rts->setValue("ProjectFile", fileName);
        this->clearArgs();

        //执行刷新操作
        //to do 

        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (operGraph) operGraph->updateAll(true);

        //刷新loadCase和Group树
        this->upGroupTreeAndTreeWidgetLoad();
    }


}

